﻿using MediatR;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.InventoryCommand;

namespace Warehouse.Read.API.Applcation.CommandHandlers.InventoryCommandHandler
{
    /// <summary>
    /// 反填盘点表
    /// </summary>
    public class FanInventoryCommandHandler : IRequestHandler<FanInventoryCommand, List<FanInventoryDTO>>
    {
        private readonly IInventoryRepository _inventRep;
        private readonly IInventProductRepository _productRep;

        public FanInventoryCommandHandler(IInventoryRepository inventRep, IInventProductRepository productRep)
        {
            _inventRep = inventRep;
            _productRep = productRep;
        }

        public async Task<List<FanInventoryDTO>> Handle(FanInventoryCommand request, CancellationToken cancellationToken)
        {
            //var res = await _inventRep.GetModelAsync(request.id);
            //1.先反填盘点 2.根据盘点id找到盘点产品的产品
            var pr = await _productRep.GetAllAsyns();
            var res=from _invent in await _inventRep.GetAllAsyns()
                     where _invent.InventoryId == request.id
                     select new FanInventoryDTO
                     {
                         InventoryId = _invent.InventoryId,
                         InventoryCode = _invent.InventoryCode,
                         MorderNumber = _invent.MorderNumber,
                         MakePerson = _invent.MakePerson,
                         MarkTime = _invent.MarkTime,
                         IRremark = _invent.IRremark,
                         InventoryState = _invent.InventoryState,
                         InventoryType = _invent.InventoryType,
                         office_id = _invent.office_id,
                         AddName=_invent.AddName,
                         AddTime=_invent.AddTime,
                         listProduct = (
                            from _p in pr
                            where _p.InventoryId == _invent.InventoryId
                            select new InventProduct
                            {
                                InventProductIdId = _p.InventProductIdId,
                                InventoryId=_p.InventoryId,
                                ProductId=_p.ProductId,
                                ProductName=_p.ProductName,
                                Number=_p.Number,
                                ProductCode =_p.ProductCode,
                                Specifications=_p.Specifications,
                                Batch=_p.Batch,
                                Unit=_p.Unit,
                                InventoryNumber=_p.InventoryNumber,
                                WarehouseLocation =_p.WarehouseLocation,
                                office_id=_p.office_id,
                                user_id=_p.user_id
                            }
                         ).ToList()

                     };
            return res.ToList();







        }























    }
}
